// $codepro.audit.disable packageNamingConvention, com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.alwaysOverridetoString.alwaysOverrideToString
/** Import Packages */
package edu.gatech.team404.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.gatech.team404.Account;
import edu.gatech.team404.AccountStatus;
import edu.gatech.team404.dal.AccountDAL;
import edu.gatech.team404.tool.Log;

/**
 * Servlet implementation class Authenticate
 * @author Michael Saul
 * @version 1.0
 */
@WebServlet("/Authenticate")
public class Authenticate extends HttpServlet {

	/** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
	protected void processRequest(HttpServletRequest request, 
								  HttpServletResponse response)
								  throws IOException, NumberFormatException {
        response.setContentType("text/html;charset=UTF-8");
        final PrintWriter out = response.getWriter();
        final AccountDAL acctDAL = new AccountDAL();
        try {
           final String TAG = "Authenticate.java";
           final int MAX_COUNT = 3;
           
           final String name = request.getParameter("name");
           final String password = request.getParameter("password"); 
           int loginCount = 0;
           
           try {
        	   loginCount = Integer.parseInt(request.getParameter("count")) + 1;
           }
           catch (NumberFormatException e) {
        	   loginCount = 1;
        	   Log.error(TAG, e);
           }

           boolean good;
           
           final Account acct = acctDAL.getAccount(name);
           if (acct == null){
        	   good = false;
           }
           else{
        	   if (acct.getStatus() != AccountStatus.SUSPENDED &&
        		   acct.getStatus() != AccountStatus.UNKNOWN) {
        		   good = acct.getPassword().equals(password);   		   
        	   }
        	   else {
        		   good = false;
        		   loginCount = -1;	//This is our cue for suspended account
        	   }
           }
           
           if(loginCount >= MAX_COUNT) {
        	   acctDAL.suspend(name);
    	   }

           if (good) {
                final Cookie userCookie = new Cookie("user", acct.getName());
                final Cookie usernameCookie = new Cookie("username", name);
                response.addCookie(userCookie);
                response.addCookie(usernameCookie);
                response.sendRedirect("default.jsp");
           } else {
        	   if (loginCount > MAX_COUNT) {
        		   loginCount = 0;
        	   }
        	   response.sendRedirect("login.jsp?count=" + loginCount);
           }
        } finally {
        	acctDAL.close();
            out.close();
        }
    }

	/** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, NumberFormatException {
        processRequest(request, response);
    } 

	/** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws NumberFormatException if a number format exception occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException, NumberFormatException {
        processRequest(request, response);
    }
    
}
